//
//  userDefault.swift
//  zzcyApp
//
//  Created by zguang on 17/7/10.
//  Copyright © 2017年 zguang. All rights reserved.
//

import Foundation
import UIKit




class UserSave{
    
    
    
   
    
    
// MARK: - 交互

// 保存，或修改
    
    
func readSex()->Bool{
        
        let userDefault = UserDefaults.standard
        if let name = userDefault.object(forKey: "sex") as? String
        {
            if name == ""{
                return false
            }
            print(name)
            return  true
            
        }else{
            
            return  false
        }
        return false
    }
    
    func saveInfo(_ id:String,_ account:String,_ password:String,_ isVip:String,_ name:String,_ sex:String,_ img:String,_ rest:String,_ consume:String,_ status:String,sessionID:String)
{
    if (0 <= id.characters.count)
    {
        let userDefault = UserDefaults.standard
        userDefault.set(id, forKey: "id")
        userDefault.set(account, forKey: "account")
        userDefault.set(password,forKey: "password")
        userDefault.set(isVip, forKey: "isvip")
        userDefault.set(name, forKey: "name")
        userDefault.set(sex, forKey: "sex")
        userDefault.set(img, forKey: "img")
        userDefault.set(rest, forKey: "rest")
        userDefault.set(consume, forKey: "consume")
        userDefault.set(status, forKey: "status")
        userDefault.set(sessionID, forKey: "Cookie")
        userDefault.synchronize()
       // let alert = UIAlertView(title: "温馨提示", message: "保存成功", delegate: nil, cancelButtonTitle: "知道了")
        //alert.show()
    }
}

// 读取
func readInfo() -> [String]
{
    let userDefault = UserDefaults.standard
    let name = userDefault.object(forKey: "id") as? String
    let code = userDefault.object(forKey: "password") as? String
     let Session = userDefault.object(forKey: "savedCookies") as? String
   let name2 = userDefault.object(forKey: "isvip") as? String
    let  img = userDefault.object(forKey: "img") as? String
    
    print(Session)
   // let alert = UIAlertView(title: "温馨提示", message: "读取成功：\(String(describing: name! + code!))", delegate: nil, cancelButtonTitle: "知道了")
//    alert.show()
    var s = [""]

    if (name != nil && code != nil)
    {
        
        s.append(name!)
        s.append(code!)
        s.append(img!)
        s.append(name2!)
        s.remove(at: 0)

        return s
    }
    
    return [""]
}

    func readLogin()->Bool{
    
        let userDefault = UserDefaults.standard
       if let name = userDefault.object(forKey: "Cookie") as? String
       {
        return true
        
       }else{
        
        return  false
        }

    }
    
    
    func readVip()->Bool{
        
        let userDefault = UserDefaults.standard
        if let name = userDefault.object(forKey: "isvip") as? String
        {
            print(name)
            if name == "1"{
            return true
            }else{
            return false
            }
            
        }else{
            
            return  false
        }
        
    }
    
    
    func readId()->Bool{
        
        let userDefault = UserDefaults.standard
        if let name = userDefault.object(forKey: "id") as? String
        {
            return true
            
        }else{
            
            return  false
        }
        return false
    }

    
    
// 删除
func deleteInfo()
{
    let useDefault = UserDefaults.standard
    useDefault.removeObject(forKey: "name")
    
   useDefault.removeObject(forKey: "password")
    useDefault.removeObject(forKey: "Cookie")
    useDefault.removeObject(forKey: "savedCookies")
    useDefault.removeObject(forKey: "account")
    useDefault.removeObject(forKey: "img")
    useDefault.removeObject(forKey: "isvip")
    useDefault.removeObject(forKey:"savedCookies")
    
    //useDefault.register(defaults: <#T##[String : Any]#>)
    print(readInfo())
    // let alert = UIAlertView(title: "温馨提示", message: "删除成功", delegate: nil, cancelButtonTitle: "知道了")
   // alert.show()
}

// MARK: - UITextFieldDelegate

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    
    textField.resignFirstResponder()
    
    return true
}

func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
    
    let text = textField.text!
    //self.saveInfo(text)
    
    return true
    }
}
